package com.attilax.zip;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipInputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import com.attilax.Closure;
import com.attilax.Closure2;

public class zip3 {

	public static void main(String[] args) throws Exception {
		try {
			final String zipFileName = "E:\\00\\product3.zip";
			readZipFile(zipFileName, "gbk", new Closure2() {

				@Override
				public Object execute(Object arg0)   {
					System.out.println("unziping " +zipFileName+"/"+arg0);
					return null;
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *  
	 * 
	 * @param zipFileName
	 * @param outputDirectory
	 * @throws Exception
	 */
	public static void readZipFile(String zipFileName, String encode,
			Closure2  clos) {

		// = "utf-8";
		String path = new File(zipFileName).getParent();
		org.apache.tools.zip.ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(zipFileName, encode);// , encode
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// new org.apache.tools.zip.ZipFile(zipFileName);
		java.util.Enumeration e = zipFile.getEntries();

		org.apache.tools.zip.ZipEntry zipEntry = null;

		while (e.hasMoreElements()) {
			zipEntry = (ZipEntry) e.nextElement();
	 
			String zipFileName2 =   zipEntry.getName();
			path_now=zipFileName2;
			if(zipEntry.isDirectory())
			{
				continue;
			}
//			if (zipEntry.getName().toLowerCase().endsWith(".zip"))
//				readZipFile(zipEntry, encode, clos);
//			else {
				clos.execute(zipFileName2);
			 
			
	//		}

		}

	}
	static String path_now="";
//	public static void readZipFile(ZipEntry zipxFile, String encode,
//			Closure2  clos) {
//
//		// = "utf-8";
////		String path = new File(zipFileName).getParent();
////		org.apache.tools.zip.ZipFile zipFile = null;
////		try {
////			zipFile = new ZipFile(zipFileName, encode);// , encode
////		} catch (IOException e1) {
////			// TODO Auto-generated catch block
////			e1.printStackTrace();
////		}
//		// new org.apache.tools.zip.ZipFile(zipFileName);
//	//	java.util.Enumeration e = zipFile.getEntries();
//		zipxFile.get
//		org.apache.tools.zip.ZipEntry zipEntry = null;
//
//		while (e.hasMoreElements()) {
//			zipEntry = (ZipEntry) e.nextElement();
//			String zipFileName2 = path + "/" + zipEntry.getName();
//			if (zipEntry.getName().toLowerCase().endsWith(".zip"))
//				readZipFile(zipFileName2, encode, clos);
//			else {
//				clos.execute(zipFileName2);
//			
//			}
//
//		}
//
//	}
}

// public static void readZipFile(String file) throws Exception {
// ZipFile zf = new ZipFile(file);
// InputStream in = new BufferedInputStream(new FileInputStream(file));
// ZipInputStream zin = new ZipInputStream(in);
// ZipEntry ze;
// while ((ze = zin.getNextEntry()) != null) {
// if (ze.isDirectory()) {
// } else {
// System.err.println("file - " + ze.getName() + " : "
// + ze.getSize() + " bytes");
// long size = ze.getSize();
// if (size > 0) {
// BufferedReader br = new BufferedReader(
// new InputStreamReader(zf.getInputStream(ze)));
// String line;
// while ((line = br.readLine()) != null) {
// System.out.println(line);
// }
// br.close();
// }
// System.out.println();
// }
// }
// zin.closeEntry();
// }
// }